use std::fmt;
use std::fmt::{Display, Formatter};

use crate::gen_title;

pub fn ch2_28_1() {
    gen_title("ch2_28_1", 18);

    println!("Hello"); // => "Hello"
    println!("Hello, {}!", "world"); // => "Hello, world!"
    println!("The number is {}", 1); // => "The number is 1"
    println!("{:?}", (3, 4)); // => "(3, 4)"
    println!("{value}", value = 4); // => "4"
    println!("{} {}", 1, 2); // => "1 2"
    println!("{:04}", 42); // => "0042" with leading zeros
}

pub fn ch2_28_2() {
    gen_title("ch2_28_2", 18);

    let s = "hello";
    println!("{}, world", s);
    let s1 = format!("{}, world", s);
    print!("{}", s1);
    print!("{}\n", "!");
}

pub fn ch2_28_3() {
    gen_title("ch2_28_3", 18);

    #[derive(Debug)]
    struct Person {
        name: String,
        age: u8,
    }

    let i = 3.1415926;
    let s = String::from("hello");
    let v = vec![1, 2, 3];
    let p = Person {
        name: "sunface".to_string(),
        age: 18,
    };
    println!("{:?}, {:?}, {:?}, {:?}", i, s, v, p);
}

pub fn ch2_28_4() {
    gen_title("ch2_28_4", 18);

    struct Person {
        name: String,
        age: u8,
    }
    impl Display for Person {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
            write!(
                f,
                "大佬在上，请受我一拜，小弟姓名{}，年芳{}，家里无田又无车，生活苦哈哈",
                self.name, self.age
            )
        }
    }

    let p = Person {
        name: "sunface".to_string(),
        age: 18,
    };
    println!("{}", p);
}

pub fn ch2_28_5() {
    gen_title("ch2_28_5", 18);

    struct Array(Vec<i32>);

    impl fmt::Display for Array {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "数组是：{:?}", self.0)
        }
    }

    let arr = Array(vec![1, 2, 3]);
    println!("{}", arr);
}

pub fn ch2_28_6() {
    gen_title("ch2_28_6", 18);

    println!("{}{}", 1, 2);
    println!("{1}{0}", 1, 2);
    println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
    println!("{1}{}{0}{}", 1, 2);
}

pub fn ch2_28_7() {
    gen_title("ch2_28_7", 18);

    println!("{argument}", argument = "test");
    println!("{name} {}", 1, name = 2);
    println!("{a} {c} {b}", a = "a", b = 'b', c = 3);
}

pub fn ch2_28_8() {
    gen_title("ch2_28_8", 18);

    let v = 3.1415926;
    println!("{:.2}", v);
    println!("{:.2?}", v);
}

pub fn ch2_28_9() {
    gen_title("ch2_28_9", 18);

    println!("Hello {:5}!", "x");
    println!("Hello {:1$}!", "x", 5);
    println!("Hello {1:0$}!", 5, "x");
    println!("Hello {:width$}!", "x", width = 5);

    println!("Hello {:1$}!{}", "x", 5);
}

pub fn ch2_28_10() {
    gen_title("ch2_28_10", 18);

    println!("Hello {:5}!", 5);
    println!("Hello {:+}!", 5);
    println!("Hello {:05}!", 5);
    println!("Hello {:05}!", -5);
}

pub fn ch2_28_11() {
    gen_title("ch2_28_11", 18);

    println!("Hello {:<5}", "x");
    println!("Hello {:>5}", "x");
    println!("Hello {:^5}", "x");

    println!("Hello {:&<5}!", "x");
}

pub fn ch2_28_12() {
    gen_title("ch2_28_12", 18);

    let v = 3.1415926;
    println!("{:.2}", v);
    println!("{:+.2}", v);
    println!("{:.0}", v);
    println!("{:.1$}", v, 4);

    let s = "hi我是sunface孙飞";
    println!("{:.3}", s);
    println!("Hello {:.*}!", 3, "abcdefg");
}

pub fn ch2_28_13() {
    gen_title("ch2_28_13", 18);

    println!("{:#b}!", 27);
    println!("{:#o}!", 27);
    println!("{}!", 27);
    println!("{:#x}!", 27);
    println!("{:#X}!", 27);
    println!("{:x}!", 27);
    println!("{:#010b}!", 27);
}

pub fn ch2_28_14() {
    gen_title("ch2_28_14", 18);

    println!("{:2e}", 10000000000_i64);
    println!("{:2E}", 10000000000_i64);
}

pub fn ch2_28_15() {
    gen_title("ch2_28_15", 18);
    let v = vec![1, 2, 3];
    println!("{:p}", v.as_ptr());
}

pub fn ch2_28_16() {
    gen_title("ch2_28_16", 18);

    println!(" Hello \"{{World}}\" ");
}
